package com.springsource.a04commonBeanPostProcessor;


import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;

public class CommonBeanPostProcessor {
    public static void main(String[] args) {

        // GenericApplicationContext 是一个最常用的应用上下文，除了BeanFactory的功能外，还支持BeanDefinition的注册。
        GenericApplicationContext context = new GenericApplicationContext();

        // 用原始的方法注册Bean
        context.registerBean("bean1", Bean1.class);
        context.registerBean("bean2", Bean2.class);
        context.registerBean("bean3", Bean3.class);
        context.registerBean("bean4", Bean4.class);

        // 设置自动装配的解析器，支持@Value 注解
        context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
        // 注册autowired注解解析后置处理器
        context.registerBean(AutowiredAnnotationBeanPostProcessor.class);

        // 注册common注解解析后置处理器，支持@Resource、@PostConstruct、@PreDestroy注解
        context.registerBean(CommonAnnotationBeanPostProcessor.class);

        // Springboot的后置处理器，支持@ConfigurationProperties注解
        ConfigurationPropertiesBindingPostProcessor.register(context.getDefaultListableBeanFactory());

        // 初始化容器，执行beanFactory的后置处理器，添加bean的后置处理器，初始化所有单例
        context.refresh();

        Bean4 bean4 = context.getBean(Bean4.class);
        System.out.println(bean4.getHome());

        // 销毁容器
        context.close();

    }
}
